package com.start.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.manager.dao.DetailDao;
import com.start.manager.domain.entity.Detail;
import com.start.manager.domain.vo.BillVO;
import com.start.manager.domain.vo.DetailVO;
import com.start.manager.service.ICompanyService;
import com.start.manager.service.IDetailService;
import com.start.manager.service.IGoodsService;
import com.start.manager.util.DateUtil;
import com.start.manager.util.MyUtil;
import com.start.manager.util.PageUtil;
import com.start.manager.util.constant.StatusConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lijian
 * @since 2019-12-20
 */
@Service
public class DetailServiceImpl extends ServiceImpl<DetailDao, Detail> implements IDetailService {

    @Autowired
    private MyUtil myUtil;

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ICompanyService companyService;

    @Override
    public PageUtil findList(DetailVO detailVO) {
        QueryWrapper<Detail> queryWrapper = initQueryWrapper(detailVO);
        if (detailVO.getSize() == 0 ){
            //获取全部的数据
            List<Detail> list = this.baseMapper.selectList(queryWrapper);
            List<DetailVO> listResult = CastDatail(list);
            return new PageUtil(list.size(),0,listResult);
        }
        //分页查询货物详情
        Page<Detail> page = new Page<>(detailVO.getPage(),detailVO.getSize());
        queryWrapper.orderByDesc("batch_date");
        Page<Detail> pageData = this.baseMapper.selectPage(page, queryWrapper);
        List<DetailVO> detailVOS = CastDatail(pageData.getRecords());
        return new PageUtil(pageData.getTotal(),pageData.getCurrent(),detailVOS);

    }

    private List<DetailVO> CastDatail(List<Detail> list) {
        List<DetailVO> listResult = new ArrayList<>();
        list.stream().forEach(detail -> {
            DetailVO detailVO = new DetailVO();
            BeanUtils.copyProperties(detail,detailVO);
            detailVO.setGoodsName(goodsService.getRedis(detail.getGoodsId()));
            detailVO.setCompanyName(companyService.getRedis(detail.getCompanyId()));
            listResult.add(detailVO);
        });
        return listResult;
    }

    private QueryWrapper<Detail> initQueryWrapper(DetailVO detailVO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (detailVO.getDataRange() != null && detailVO.getDataRange().length > 0){
            queryWrapper.ge("batch_date",detailVO.getDataRange()[0]);
            queryWrapper.le("batch_date",detailVO.getDataRange()[1]);
        }
        if (StringUtils.isNotBlank(detailVO.getGoodsId())){
            queryWrapper.eq("goods_id",detailVO.getGoodsId());
        }
        if (!Objects.isNull(detailVO.getType())) {
            queryWrapper.eq("type",detailVO.getType());
        }
        if (StringUtils.isNotBlank(detailVO.getCompanyId())) {
            queryWrapper.eq("company_id",detailVO.getCompanyId());
        }
        queryWrapper.eq("status", StatusConstant.USE);
        return queryWrapper;
    }

    @Override
    public boolean saveDetail(DetailVO detailVO) {

        Detail detail = new Detail();
        if (StringUtils.isBlank(detailVO.getId())) {
            detail.setId(myUtil.generateId("D"));
        }
        BeanUtils.copyProperties(detailVO,detail);
        detail.setCurrTime(DateUtil.getNowTime_EN());
        detail.setStatus(StatusConstant.USE);
        int insert = this.baseMapper.insert(detail);
        return insert > 0 ? true : false;
    }

    @Override
    public List<Map<String,Object>> findHomeTotal(String currentWeeksDay) {
        List<Map<String,Object>> list = this.baseMapper.getHomeData(currentWeeksDay);
        if (list.size() > 2) {
            return list;
        }
        if (list.size() == 1){
            Map<String, Object> stringObjectMap = list.get(0);
            int type = (int)list.get(0).get("type");
            Map<String, Object> map = new HashMap<>();
            if (type == 1){
                map.put("type",2);
                map.put("totalPrice",new BigDecimal(0));
            }else{
                map.put("type",1);
                map.put("totalPrice",new BigDecimal(0));
            }
            list.add(map);
        }
        return list;
    }

    @Override
    public BigDecimal findMonthData(String type, String firstDay, String lastDay) {
        DecimalFormat df1 = new DecimalFormat("0.00");
        Double currentMonth = this.baseMapper.getCurrentMonth(type, firstDay, lastDay);
        return new BigDecimal(df1.format(new BigDecimal(currentMonth == null ? 0d : currentMonth)));
    }

    @Override
    public Map<String, Object> findData(BillVO billVO) {
        Map<String, Object> map = new HashMap<>();
        Double inBill ;
        Double outBill;
        if (billVO.getDataRange() == null || billVO.getDataRange().length != 2){
            inBill = this.baseMapper.fingBill("1",null,null);
            outBill = this.baseMapper.fingBill("2",null,null);
            map.put("inBill",inBill);
            map.put("outBill",outBill);
            return map;
        }
        inBill = this.baseMapper.fingBill("1",billVO.getDataRange()[0],billVO.getDataRange()[1]);
        outBill = this.baseMapper.fingBill("2",billVO.getDataRange()[0],billVO.getDataRange()[1]);

        map.put("inBill",inBill);
        map.put("outBill",outBill);
        return map;
    }
}
